package com.example.androidcomplete02.sqlite;

import android.app.Activity;
import android.app.LoaderManager.LoaderCallbacks;
import android.content.ContentValues;
import android.content.Context;
import android.content.Loader;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.example.androidcomplete02.R;

public class EnglishWord_CursorLoader_Adapter extends Activity implements
		LoaderCallbacks<Cursor> {
	private WordDBManager mDbManager;
	private ListView list;
	private MyCursorAdapter mAdapter;
	private static final String[] columns = { "_id", "eng", "han" };

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch26_englishwordex);
		mDbManager = WordDBManager.getInstance(this);
		list = (ListView) findViewById(R.id.word_list);
		getLoaderManager().initLoader(0, null, this);
	}

	public void mOnClick(View v) {
		String whereClause = null;
		String[] whereArgs;
		switch (v.getId()) {
		case R.id.insertbtn:
			EditText eng = (EditText) findViewById(R.id.engedit);
			EditText han = (EditText) findViewById(R.id.handedit);
			ContentValues addRowValue = new ContentValues();
			addRowValue.put("eng", eng.getText().toString());
			addRowValue.put("han", han.getText().toString());
			mDbManager.insert(addRowValue);
			eng.setText("");
			han.setText("");
			break;
		case R.id.selectbtn:
			getLoaderManager().restartLoader(0, null, this);
			break;
		case R.id.deletebtn:
			EditText num = (EditText) findViewById(R.id.deledid);
			whereClause = "_id=?";
			whereArgs = new String[1];
			whereArgs[0] = num.getText().toString();
			mDbManager.delete(whereClause, whereArgs);
			num.setText("");
			break;
		case R.id.updatebtn:
			EditText id = (EditText) findViewById(R.id.update_id);
			EditText uphan = (EditText) findViewById(R.id.update_han);
			ContentValues updateRowValue = new ContentValues();
			updateRowValue.put("han", uphan.getText().toString());
			whereClause = "_id=?";
			whereArgs = new String[1];
			whereArgs[0] = id.getText().toString();
			mDbManager.update(updateRowValue, whereClause, whereArgs);
			id.setText("");
			uphan.setText("");
			break;
		}
		getLoaderManager().restartLoader(0, null, this);
	}

	@Override
	public Loader<Cursor> onCreateLoader(int id, Bundle args) {
		return new EnglishWordCursorLoader(this);
	}

	@Override
	public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
		mAdapter = new MyCursorAdapter(this, data, true);
		list.setAdapter(mAdapter);
	}

	@Override
	public void onLoaderReset(Loader<Cursor> loader) {
		mAdapter.swapCursor(null);
	}

	public final static class EnglishWordCursorLoader extends
			SimpleCursorLoader {
		private WordDBManager mHelper;

		public EnglishWordCursorLoader(Context context) {
			super(context);
			mHelper = WordDBManager.getInstance(context);
		}

		@Override
		protected Cursor loadCursor() {
			return mHelper.select(columns, null, null, null, null, null);
		}
	}

	public class MyCursorAdapter extends CursorAdapter {
		private LayoutInflater mInflater;

		public MyCursorAdapter(Context context, Cursor c, boolean autoRequery) {
			super(context, c, autoRequery);
			mInflater = LayoutInflater.from(context);
		}

		/*
		 * class ViewHolder{ TextView mListNum; TextView mListEng; TextView
		 * mListHan; }
		 */
		@Override
		public View newView(Context context, Cursor cursor, ViewGroup parent) {
			View itemLayout = mInflater.inflate(R.layout.ch26_list_row, null);
			/*
			 * ViewHolder viewHolder = new ViewHolder(); viewHolder.mListNum =
			 * (TextView) itemLayout.findViewById(R.id.listnum);
			 * viewHolder.mListEng = (TextView)
			 * itemLayout.findViewById(R.id.listeng); viewHolder.mListHan =
			 * (TextView) itemLayout.findViewById(R.id.listhan);
			 * itemLayout.setTag(viewHolder);
			 */
			return itemLayout;
		}

		@Override
		public void bindView(View view, Context context, Cursor cursor) {
			TextView mListNum = (TextView) view.findViewById(R.id.listnum);
			TextView mListEng = (TextView) view.findViewById(R.id.listeng);
			TextView mListHan = (TextView) view.findViewById(R.id.listhan);
			mListNum.setText(cursor.getString(cursor.getColumnIndex("_id")));
			mListEng.setText(cursor.getString(cursor.getColumnIndex("eng")));
			mListHan.setText(cursor.getString(cursor.getColumnIndex("han")));
			/*
			 * ViewHolder viewHolder = (ViewHolder) view.getTag(); String id =
			 * cursor.getString(cursor.getColumnIndex("_id")); String eng =
			 * cursor.getString(cursor.getColumnIndex("eng")); String han =
			 * cursor.getString(cursor.getColumnIndex("han"));
			 * viewHolder.mListNum.setText(id);
			 * viewHolder.mListEng.setText(eng);
			 * viewHolder.mListHan.setText(han);
			 */
		}
	}
}